/*************************************************************************

Framework\System.Windows.Automation.cpp

	-by Miles Chen (stainboyx@hotmail.com) 2008-12-27

*************************************************************************/

#pragma once

#include "commdef-int.h"

N System{N Windows{N Automation{


#define SHORT_TIME 200
#define TRY_MAX_TIMES 50

	BOOL CALLBACK EnumThreadWindowProc(HWND hWnd, LPARAM lParam)
	{
		*(HWND*)lParam = hWnd;
		return FALSE;
	}

	BOOL GetProcessMainWindowHandle(LPPROCESS_INFORMATION ppi, LPCWSTR lpszClass, LPCWSTR lpszCaption, OUT HWND* handle)
	{
		int tryTime = 0;
		WCHAR szClass[MAX_CHAR];
		WCHAR szCaption[MAX_CHAR];
		while(TRUE)
		{
			Sleep( SHORT_TIME );
			EnumThreadWindows(ppi->dwThreadId, (WNDENUMPROC)&EnumThreadWindowProc, (LPARAM)handle);
			if( tryTime++ == TRY_MAX_TIMES )
			{
				return FALSE;
			}
			else
			{
				if(*handle)
				{
					if(!lpszClass && !lpszCaption)
						break;
					else if(lpszClass && lpszCaption)
					{
						GetWindowTextW(*handle, szCaption, MAX_CHAR);
						GetClassNameW(*handle, szClass, MAX_CHAR);

						if(System::String::Compare(szCaption, lpszCaption) == 0
							&& System::String::Compare(szClass, lpszClass) == 0)
							break;
					}
					else if(lpszClass)
					{
						GetClassNameW(*handle, szClass, MAX_CHAR);

						if(System::String::Compare(szClass, lpszClass) == 0)
							break;
					}
					else if(lpszCaption)
					{
						GetWindowTextW(*handle, szCaption, MAX_CHAR);

						if(System::String::Compare(szCaption, lpszCaption) == 0)
							break;
					}
				}
			}
		}

		return TRUE;
	}

	BOOL CALLBACK EnumDesktopWindowProc(HWND hWnd, LPARAM lParam)
	{
		PLONG p = (PLONG)lParam;
		DWORD pid = 0;

		GetWindowThreadProcessId(hWnd, &pid);
		if(pid == p[1])
		{
			p[0] = (LONG)hWnd;
			return FALSE;
		}

		return TRUE;
	}

	HWND GetProcessMainWindowHandle(HANDLE hProcess)
	{
		LONG p[] = { 0, GetProcessId(hProcess) };

		int tryTime = 0;
		while(tryTime++ < TRY_MAX_TIMES)
		{
			Sleep(SHORT_TIME);
			EnumWindows(EnumDesktopWindowProc, (LPARAM)p);

			if(p[0])
				break;
		}

		return (HWND)p[0];
	}

	BOOL GetFirstChildHandle(HWND hwndParent, OUT HWND* handle)
	{
		int tryTime = 0;
		do
		{
			Sleep( SHORT_TIME );
			*handle = FindWindowExW(hwndParent, NULL, NULL, NULL);
			if( tryTime++ == TRY_MAX_TIMES )
			{
				return FALSE;
			}
		}while(!*handle);

		return TRUE;
	}

	BOOL GetChildHandle(HWND hwndParent, int nIDDlgItem, OUT HWND* handle)
	{
		int tryTime = 0;
		do
		{
			Sleep( SHORT_TIME );
			*handle = GetDlgItem(hwndParent, nIDDlgItem);
			if( tryTime++ == TRY_MAX_TIMES )
			{
				return FALSE;
			}
		}while(!*handle);

		return TRUE;
	}

	BOOL WaitForEnable(HWND hWnd)
	{
		int tryTime = 0;
		do
		{
			Sleep( SHORT_TIME );
			if( tryTime++ == TRY_MAX_TIMES )
			{
				return FALSE;
			}
		}while(GetWindowLongW(hWnd, GWL_STYLE) & WS_DISABLED);

		return TRUE;
	}

}}}